package structures.tree2;

public class ArrayBinaryTreeDemo {
    public static void main(String[] args) {
        int[] dataArray = {1, 3, 4, 7, 7, 8, 8, 10, 2};
        IntBinaryList intBinaryList = new IntBinaryList(dataArray);
        intBinaryList.preOrder();
        System.out.println("---------");
        intBinaryList.infixOrder();
    }
}

class IntBinaryList {
    int[] dataArray;

    public IntBinaryList(int[] dataArray) {
        this.dataArray = dataArray;
    }

    public void preOrder() {
        preOrder(0);
    }

    /**
     * 前序遍历
     * @param index
     */
    private void preOrder(int index) {
        if (dataArray == null ||
            index >= dataArray.length ||
            dataArray.length == 0) {
            return;
        }

        System.out.println(dataArray[index]);

        preOrder(index * 2 + 1);
        preOrder(index * 2 + 2);
    }

    public void infixOrder() {
        infixOrder(0);
    }

    /**
     * 中序遍历
     */
    private void infixOrder(int index) {

        if (dataArray == null ||
                index >= dataArray.length ||
                dataArray.length == 0) {
            return;
        }

        infixOrder(index * 2 + 1);
        System.out.println(dataArray[index]);
        infixOrder(index * 2 + 2);
    }
}
